/**
 * Created by Administrator on 2016/8/22.
 */
NodeList.prototype.forEach = Array.prototype.forEach;

var path_recieve = 'http://pm.ht-home.cn/api/others/getRecipeInfoForShare.json?projectId=' + window.currentProjectId;
var xhr_recieve = null;

if (window.XMLHttpRequest) {
    // 非IE内核
    xhr_recieve = new XMLHttpRequest();
}

xhr_recieve.open('get', path_recieve, true);
xhr_recieve.send(null);
xhr_recieve.onreadystatechange = function () {
    if (xhr_recieve.readyState === 4) {
        if (xhr_recieve.status === 200) {
            var jsonData_recieve = JSON.parse(xhr_recieve.responseText);
            BindData_recieve(jsonData_recieve);
        }
    }
};


function BindData_recieve(jsonData) {
    var data = jsonData.data.customerMsg;
    document.getElementById("name_recieve").innerHTML = typeof data.customerName === 'undefined' ? ''  : data.customerName;
    document.getElementById("man_recieve").innerHTML =  typeof data.contacts === 'undefined' ? ''  : data.contacts;
    document.getElementById("area_recieve").innerHTML =  typeof data.regionCode === 'undefined' ? ''  : data.regionCode;
    document.getElementById("address_recieve").innerHTML =  typeof data.address === 'undefined' ? ''  : data.address;

    //发货信息
    var sendGoodsInfo = jsonData.data.sendGoodsInfo;
    document.getElementById("date_sendout").innerHTML = typeof sendGoodsInfo.sendTime === 'undefined' ? ''  : sendGoodsInfo.sendTime;
    document.getElementById("estimated_date_sendout").innerHTML = typeof sendGoodsInfo.receipeTime === 'undefined' ? ''  : sendGoodsInfo.receipeTime;
    document.getElementById("info_sendout").innerHTML = typeof sendGoodsInfo.logisticsInfo === 'undefined' ? ''  : sendGoodsInfo.logisticsInfo;

    var dataList = jsonData.data.dataList;
    for (var i = 0; i < dataList.length; i++) {
        if (dataList[i].categoryCode != "A0101") {
            var newNode1 = document.createElement('li');
            newNode1.className = 'list-title';
            var str = dataList[i].itemCode;
            var src = '<img class="title-img" src="img/icon-tile.png">';

            if (dataList[i].categoryCode == "A030403") {
                src += '<span class="title-span">' + dataList[i].category2 + '  ' + dataList[i].itemCode.substr(str.length - 2, 2) + '</span>';
            } else {
                src += '<span class="title-span">' + dataList[i].category2 + '</span>';
            }
            newNode1.innerHTML = src;
            var newNode2 = document.createElement('li');
            newNode2.className = "list-content";
            var src1;
            src1 = '<span class="content-span-left">' + dataList[i].itemCode + '</span>'
            if (typeof dataList[i].itemQty == "undefined") {
                src1 = '<span class="content-span-left">总数0套</span>';
            } else if (dataList[i].categoryCode == "A030408") {
                src1 = '<span class="content-span-left">若干</span>';
            } else {
                src1 = '<span class="content-span-left">总数' + dataList[i].itemQty + '套</span>';
            }
            if (typeof dataList[i].receivedQty == "undefined") {
                src1 += '<span class="content-span_recieve">收货0套</span>';
            } else if (dataList[i].categoryCode == "A030408") {
                src1 = '<span class="content-span-left"></span>';
            } else {
                src1 += '<span class="content-span_recieve">收货' + (dataList[i].receivedQty || "") + '套</span>';
            }
            newNode2.innerHTML = src1;

            var newNode3 = document.createElement('li');
            newNode3.className = "list-content";
            var src2 = '<span class="content-span-left">' + dataList[i].itemCode + '</span>';
            newNode3.innerHTML = src2;

            document.getElementById("content_recieve").appendChild(newNode1);
            document.getElementById("content_recieve").appendChild(newNode3);
            document.getElementById("content_recieve").appendChild(newNode2);

        } else {
            var src3 = '<li class="list-content"><span class="content-span-left">总数' + dataList[i].itemQty + '套</span>';
            src3 += '<span class="content-span_recieve">收货' + dataList[i].receivedQty + '套</span></li>';
            // src3 += '<li class="list-content"><span class="content-span-left">' + dataList[i].itemCode + '</span></li>';
            document.getElementById("module_content_recieve").innerHTML = src3;
        }
    }

    var nbqContent = document.getElementById("nbq_content_recieve");
    var moduleContent = document.getElementById("module_content_recieve");
    var nbqNum = 1;
    var moduleNum = 1;
    for (var i = 0; i < jsonData.data.serialList.length; i++) {
        var newNode = document.createElement('div');
        var html = '<div class="line"></div>';
        var value = jsonData.data.serialList[i];
        if (value.categoryCode == "A0101") {
            //组件
            html += '<li class="list-content"><span class="content-span-high">组件' + moduleNum + '</span>';
            html += '<span class="content-span_recieve">共一套</span></li>';
            html += '<div class="line"></div>';
            html += '<li class="list-content"><span class="content-span-left">' + value.serialNumber + '</span></li>';
            moduleNum++;
            newNode.innerHTML = html;
            moduleContent.appendChild(newNode);
        } else if (value.categoryCode == "A0302") {
            //逆变器
            html += '<li class="list-content"><span class="content-span-high">逆变器' + nbqNum + '</span>';
            html += '<span class="content-span_recieve">共一套</span></li>';
            html += '<div class="line"></div>';
            html += '<li class="list-content">' + ( typeof value.itemCode  === 'undefined' ? ''  : value.itemCode ) + '</li>';
            if (typeof value.itemCode  !== 'undefined') {
                html += '<div style="height: 1px; background-color: lightgray; margin-left: 16px;"></div>';
            }
            html += '<li class="list-content"><span class="content-span-left">序列号</span>';
            html += '<span class="content-span_recieve">' + ( typeof value.serialNumber  === 'undefined' ? ''  :value.serialNumber) + '</span></li>';
            html += '<li class="list-content"><span class="content-span-left">通讯模块</span>';
            html += '<span class="content-span_recieve">' + ( typeof value.serialNumber2  === 'undefined' ? ''  : value.serialNumber2) + '</span></li>';
            html += '<li class="list-content"><span class="content-span-left">SIM卡1</span>';
            html += '<span class="content-span_recieve">' + ( typeof value.sim1  === 'undefined' ? ''  : value.sim1) + '</span></li>';
            if (typeof value.sim2 != "undefined") {
                html += '<li class="list-content"><span class="content-span-left">SIM卡2</span>';
                html += '<span class="content-span_recieve">' + (value.sim2 || "") + '</span></li>';
            }

            nbqNum++;
            newNode.innerHTML = html;
            nbqContent.appendChild(newNode);
        }

    }
}
